<template>
	<el-form ref="form" :model="form" label-width="110px" @submit.prevent="onSubmit" style="margin:20px;width:60%;min-width:600px;" >

		<el-form-item style="color:red;" id="xxdfsdfsd">
			<span>请先将导出的订单表格中列为订单号复制并粘贴到下面输入框中,点击确定即可</span>
			<br>
			<span>此操作将直接影响订单成功或失败，请谨慎操作,以免造成损失</span>
		</el-form-item>
		<el-form-item label="平台上游订单号">
			<el-input  type="textarea" v-model="form.orderIds" :rows="15"
			placeholder="
			批量处理的格式为:订单号 每行一个 如:
			xxxx
			xxxx
 			xxxx"  ></el-input>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" v-on:click="onSubmit">批量失败</el-button>
			<el-button type="primary" v-on:click="onrestBatch">批量成功</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
	import {
		findAllDownShopInfo,
		findAllDownProduct,
		findDownShopInfoKey,
		getConfig,
		failtUpOrder, successUpOrder
	} from '../../api/api';
    var crypto = require('crypto');
	import NProgress from 'nprogress'
	import { Loading } from 'element-ui';


	export default {
		data() {
//     {
//			    "orderId ":"xxxxxxx",
//			    "uid":"xxxxxxxxxxxx",
//			    "sign":"3183496d7f0cb397b819bdc89a11c0fc",
//			    "pid":"4028b8815f7d4ae2015f86ee6aa70009",
//			    "phone":"18000001111",
//			    "notify":"http://xxx:port/call",
//			    "ts":"20170928115522"
//			}
			return {
				form: {
					orderIds: ''
				},
                users:[],
                products:[]
			}
		},
		methods: {
			onSubmit() {
				const orders = this.form.orderIds.split("\n");
				let sels = [];
				for(var i = 0 ; i < orders.length;i++){
					sels.push({upOrderId:orders[i]})
				}
				this.$confirm('确认批量失败订单吗？', '提示', {type: 'warning'}).then(() => {
					let loadingInstance = Loading.service({fullscreen: true,text:"努力加载中"});

					let para = {
						upOrders: sels
					};
					failtUpOrder(para).then((datas) => {
						loadingInstance.close();
						this.listLoading = false;
						let {message, code, data} = datas;
						if (code !== "0") {
							this.$message({
								message: message,
								type: 'error'
							});
						}
						this.$message({
							message: message,
							type: 'info'
						});
						NProgress.done();
					});
				});
			},
			onrestBatch:function(){
				NProgress.start();
				const orders = this.form.orderIds.split("\n");
				let sels = [];
				for(var i = 0 ; i < orders.length;i++){
					sels.push({upOrderId:orders[i]})
				}
				this.$confirm('确认批量成功订单吗？', '提示', {type: 'warning'}).then(() => {
					let para = {
						upOrders: sels
					};
					successUpOrder(para).then((datas) => {
						this.listLoading = false;
						let {message, code, data} = datas;
						if (code !== "0") {
							this.$message({
								message: message,
								type: 'error'
							});
						}
						this.$message({
							message: message,
							type: 'info'
						});
						NProgress.done();
					});
				});
			}
			,findAllDownShopInfo: function () {
                let para = {useruuid: JSON.parse(sessionStorage.getItem("user")).useruuid};
                findAllDownShopInfo(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.users = data;
                    }
                });
            },getConfig(){
                let para = {useruuid: JSON.parse(sessionStorage.getItem("user")).useruuid};
                getConfig(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.form.orderId = data.testorderprefix+new Date().getTime();
                        this.form.notify = data.testcallbackurl;
                    }
                });
			},
			userChange: function (shopuuid) {
				this.form.shopuuid = shopuuid;
                let para = {useruuid: this.form.uid};
                findAllDownProduct(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.products = data;
                    }
                });
                findDownShopInfoKey(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                       this.form.key = data;
                    }
                });
			},productChange:function(productuuid){
				for(var i = 0 ; i < this.products.length ; i++){
				    if(this.products[i].downprouuid == productuuid){
                        this.form.amount = this.products[i].price;
					}
				}
			},
			signMethod(){
                var md5 = crypto.createHash('md5');
                var string = "notify="+this.form.notify+"&orderId="+this.form.orderId+"&phone="+this.form.phone+"&pid="+this.form.pid+"&ts="+this.form.ts+"&uid="+this.form.uid+"&key="+this.form.key;
				this.form.sign =  md5.update(string).digest('hex');
			}
		},mounted() {
            this.findAllDownShopInfo();
            this.getConfig();
        }
	}

</script>
